<template>
	<view :style="themeColor">
		<view class="page">
			<view class="flex benben-position-layout flex flex-wrap align-center courseCatalog_flex_0"
				:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
				<view class='flex flex-wrap align-center justify-between flex-sub courseCatalog_fd0_0'>
					<view class='flex flex-wrap align-center courseCatalog_fd0_0_c0' @tap.stop="handleJumpDiy"
						data-type="back" data-url="1">
						<text class='fu-iconfont2  courseCatalog_fd0_0_c0_c0'>&#xE794;</text>
					</view>
					<view class='flex flex-wrap align-stretch justify-center'>
						<text class='courseCatalog_fd0_0_c1_c0'>课程目录</text>
					</view>
					<view class='flex flex-wrap align-center justify-end courseCatalog_fd0_0_c0' @tap.stop="bianji">
						<text v-if="!isEdit">编辑</text>
						<text v-else>完成</text>
					</view>
					
				</view>

			</view>
			<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
			
			<view class="loading_css" v-if="isShow">
				<u-loading size="36" color="#3090FF"></u-loading>
				<view>加载中</view>
			</view>
			
			<!---flex布局flex布局开始-->
			<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout courseCatalog_flex_1" v-else>
				<view class='flex flex-direction flex-wrap align-stretch' v-for="(item, index) in directoryList" :key="index">
					<view class='flex flex-wrap align-center courseCatalog_fd1_0_c0'>
						<text class='courseCatalog_fd1_0_c0_c0'>{{item.name}}</text>
					</view>
					<view class='flex align-center' v-for="(v, k) in item.children" :key="k">
						<view class='flex align-center myCourse_fd5_0_c0' v-if="isEdit" @tap.stop="selectEdit(index, k)">
							<image v-if="v.isSelect" class='myCourse_fd5_0_c0_c0' mode="aspectFill" :src='STATIC_URL+"3.png"'></image>
							<image v-else class='myCourse_fd5_0_c0_c0' mode="aspectFill" :src='STATIC_URL+"135.png"'></image>
						</view>
						<view style="flex: 1;" class='flex align-stretch justify-between courseCatalog_fd1_0_c1'  @tap.stop="viewVideo(v.aid, v.shipin, v.is_download, v.name, v.learn)">
							<view class='flex align-center'>
								<view class='flex flex-wrap'>
									<image class='courseCatalog_fd1_0_c1_c0_c0_c0' mode="aspectFill"
										:src='STATIC_URL+"513.png"'></image>
									<view class='flex flex-wrap align-center courseCatalog_fd1_0_c1_c0_c0_c1'>
										<text class='courseCatalog_fd1_0_c1_c0_c0_c1_c0'>{{v.name}}</text>
									</view>
								</view>
							</view>
							<view class='flex flex-wrap align-stretch courseCatalog_fd1_0_c2_c1'>
								<image class='courseCatalog_fd1_0_c1_c0_c0_c0' mode="aspectFit" :src='STATIC_URL+"515.png"'>
								</image>
							</view>
						</view>
					</view>
					
				</view>
			</view>
			
			<view class="noData" v-if="directoryList.length == 0 && !isShow">
				<image :src='STATIC_URL+"no.png"'></image>
			</view>
			<!---flex布局flex布局结束-->
			
			<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout" v-if="isEdit">
				<view class='flex flex-wrap align-center justify-between myCourse_fd1_0'>
					<view class='flex flex-wrap align-center' @tap.stop="selectTotal">
						<image v-if="isTotal" class='myCourse_fd1_0_c0_c0' mode="aspectFit" :src='STATIC_URL+"3.png"'></image>
						<image v-else class='myCourse_fd1_0_c0_c0' mode="aspectFit" :src='STATIC_URL+"135.png"'></image>
						<text class='myCourse_fd1_0_c0_c2'>全选</text>
					</view>
					<view class='flex flex-wrap align-center justify-center myCourse_fd1_0_c1' @tap.stop="deleteCache">
						<text class='self-center myCourse_fd1_0_c1_c0'>删除</text>
					</view>
				</view>
			</view>
			<view style="height: 100rpx;" v-if="isEdit"></view>
			
			
			<!---flex布局flex布局结束-->
			<benben-popup v-model="popupShow1702371418499" :mask="true" :mask-close-able="false" mode='center'>
				<!---清除本地缓存flex布局开始-->
				<view class="flex benben-flex-layout flex-wrap align-center">
					<view class='flex flex-direction flex-wrap align-center myCourse_fd2_0'>
						<text class='myCourse_fd2_0_c0'>提示</text>
						<text class='myCourse_fd2_0_c1'>确定删除选中的视频？</text>
						<view class='flex align-center myCourse_fd2_0_c2'>
							<button class='myCourse_fd2_0_c2_c0' @tap.stop="popupShow1702371418499=false">取消</button>
							<button class='myCourse_fd2_0_c2_c1' @tap.stop="confirmDel">确认</button>
						</view>
					</view>
				</view>
				<!---清除本地缓存flex布局结束-->
			</benben-popup>
		</view>
	</view>
</template>
<script>
	export default {
		components: {},
		data() {
			return {
				isShow: true,
				id: "",
				directoryList: [],
				is_network: 0,
				
				saveFileList: [],
				isEdit: false,
				isTotal: false,
				popupShow1702371418499: false,
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},

		},
		watch: {},
		onLoad(options) {
			let {id} = options;
			if (id !== undefined) this.id = id;
			
			let _this = this;
			uni.getNetworkType({
				success: function (res) {
					// console.log(res);
					if (res.networkType == 'none') {
						_this.is_network = 0;
					} else{
						_this.is_network = 1;
					}
				}
			});
			uni.onNetworkStatusChange(function (res) {
				if (res.networkType == 'none') {
					_this.is_network = 0;
				} else{
					_this.is_network = 1;
				}
			});
			
		},
		onUnload() {

		},
		onReady() {

		},
		onShow() {
			this.isShow = true;
			this.getCourseCatalog();
			this.saveFileList = uni.getStorageSync('saveFileList');
			// console.log(this.saveFileList);
		},
		onHide() {

		},
		onResize() {

		},
		onPullDownRefresh() {

		},
		onReachBottom(e) {

		},
		onPageScroll(e) {

		},
		methods: {
			// 缓存目录  post658582a4b4ba5
			getCourseCatalog() {
				this.$api.post(global.apiUrls.post658582a4b4ba5, {
					aid: this.id
				}).then(res => {
					console.log(res);
					this.isShow = false;
					if (res.data.code == 1) {
						this.directoryList = res.data.data;
						if (this.directoryList.length > 0) {
							this.directoryList.forEach((v, k) => {
								if (v.children.length > 0) {
									v.children.forEach((v1, k1) => {
										v1.isSelect = false;
									})
								}
							})
						}
					} else {
						this.$message.info(res.data.msg);
					}
				})
			},
			selectEdit(index, index1) {
				this.directoryList[index].children[index1].isSelect = !this.directoryList[index].children[index1].isSelect;
				let num = 0;
				this.directoryList.forEach((v, k) => {
					if (v.children.length > 0) {
						v.children.forEach((v1, k1) => {
							if (!v1.isSelect) {
								num += 1;
							}
						})
					}
				})
				if (num != 0) {
					this.isTotal = false;
				} else {
					this.isTotal = true;
				}
				this.$forceUpdate();
			},
			bianji() {
				this.isEdit = !this.isEdit;
			},
			selectTotal() {
				if (this.directoryList.length == 0) return;
				this.isTotal = !this.isTotal;
				if (this.isTotal) {
					this.directoryList.forEach((v, k) => {
						if (v.children.length > 0) {
							v.children.forEach((v1, k1) => {
								v1.isSelect = true;
							})
						}
					})
				} else {
					this.directoryList.forEach((v, k) => {
						if (v.children.length > 0) {
							v.children.forEach((v1, k1) => {
								v1.isSelect = false;
							})
						}
					})
				}
				this.$forceUpdate();
			},
			// 删除缓存
			deleteCache() {
				let num = 0;
				this.directoryList.forEach((v, k) => {
					if (v.children.length > 0) {
						v.children.forEach((v1, k1) => {
							if (v1.isSelect) {
								num += 1;
							}
						})
					}
				})
				console.log(num);
				if (num == 0) {
					this.$message.info('请选择删除的课程');
					return;
				}
				this.popupShow1702371418499 = true;
			},
			// 确定删除  post658582e7898b7
			confirmDel() {
				this.popupShow1702371418499 = false;
				let chapter_id = [];
				this.directoryList.forEach((v, k) => {
					if (v.children.length > 0) {
						v.children.forEach((v1, k1) => {
							if (v1.isSelect) {
								chapter_id.push(v1.aid);
							}
						})
					}
				})
				
				this.$api.post(global.apiUrls.post658582e7898b7, {
					chapter_id: chapter_id.join(',')
				}).then(res => {
					// console.log(res);
					this.$message.info(res.data.msg);
					if (res.data.code == 1) {
						this.isEdit = false;
						this.isTotal = false;
						this.isShow = true;
						this.getCourseCatalog();
						
						// 删除缓存中的数据
						this.saveFileList.forEach((v, k) => {
							course_id.forEach((v1, k1) => {
								if (v1 == v.id && this.id == v1.course_id) {
									this.saveFileList.splice(k, 1);
								}
							})
						})
						if (this.saveFileList.length == 0) {
							this.saveFileList = "";
						}
						uni.setStorageSync('saveFileList', this.saveFileList);
					}
				})
			},
			viewVideo(id, url, is_download, name, learn) {
				let _this = this;
				let videoUrl = {
					url: "",
					id: id,
					is_download: is_download,
					name: name,
					learn: learn
				};
				let saveFileList = [];
				if (uni.getStorageSync('saveFileList')) {
					saveFileList = uni.getStorageSync('saveFileList');
				}
				if (_this.is_network == 0) {
					if (saveFileList.length > 0) {
						let index = -1;
						saveFileList.forEach((v, k) => {
							if (v.id == id) {
								index = k;
							}
						})
						if (index == -1) {
							_this.$message.info('视频暂未缓存');
							return;
						} else {
							videoUrl.learn = 0;
							videoUrl.url = saveFileList[index].url;
							uni.setStorageSync('videoUrl', videoUrl);
							uni.navigateTo({
								url: '/pages/sy/seeVideo/seeVideo'
							})
						}
					} else {
						_this.$message.info('视频暂未缓存');
						return;
						
					}
				} else{
					videoUrl.url = url;
					uni.setStorageSync('videoUrl', videoUrl);
					uni.navigateTo({
						url: '/pages/sy/seeVideo/seeVideo'
					})
				}
			},
		}
	};
</script>
<style lang="scss" scoped>
	
	.myCourse_fd1_0_c1_c0 {
		font-size: 32rpx;
		color: var(--benbenFontColor3);
	}
	
	.myCourse_fd1_0_c1 {
		background: #FF5050;
		line-height: 98rpx;
		width: 232rpx;
		height: 98rpx;
		background-size: 100% auto;
	}
	
	.myCourse_fd1_0_c0_c2 {
		font-size: 26rpx;
		color: rgba(150, 150, 150, 1);
		margin: 0rpx 0rpx 0rpx 16rpx;
	}
	
	.myCourse_fd1_0_c0_c0 {
		width: 40rpx;
		height: 40rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}
	
	.myCourse_fd1_0 {
		padding: 0rpx 0rpx 0rpx 32rpx;
		width: 100%;
		height: 98rpx;
		position: fixed;
		bottom: calc(0rpx + var(--window-bottom));
		box-shadow: 0px -3rpx 6rpx 1px rgba(0,0,0,0.07);
	}
	
	.myCourse_fd2_0_c2_c1 {
		background: var(--benbenbgColor1);
		font-size: 32rpx;
		color: var(--benbenFontColor4);
		width: 270rpx;
		height: 101rpx;
		line-height: 101rpx;
		font-weight: 500;
	}
	
	.myCourse_fd2_0_c2_c0 {
		border-right: 1px solid #eee;
		background: #fff;
		font-size: 32rpx;
		color: rgba(153, 153, 153, 1);
		width: 270rpx;
		height: 101rpx;
		line-height: 101rpx;
		font-weight: 400;
	}
	
	.myCourse_fd2_0_c2 {
		border-top: 1px solid #eee;
		margin: 51rpx 0rpx 0rpx 0rpx;
	}
	
	.myCourse_fd2_0_c1 {
		line-height: 44rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #333333;
		text-align: center;
		margin: 32rpx 40rpx 0rpx 40rpx;
	}
	
	.myCourse_fd2_0_c0 {
		line-height: 44rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #333333;
		margin: 40rpx 0rpx 0rpx 0rpx;
	}
	
	.myCourse_fd2_0 {
		background: #fff;
		width: 540rpx;
		background-size: 100% auto !important;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}
	.myCourse_fd5_0_c0_c0 {
		width: 36rpx;
		height: 36rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}
	
	.myCourse_fd5_0_c0 {
		margin: 20rpx 16rpx 0rpx 0rpx;
	}
	
	
	
	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #FFFFFF;
		background-size: 100% auto;
	}

	.courseCatalog_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.courseCatalog_fd0_0_c2_c0 {
		width: 48rpx;
		height: 48rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.courseCatalog_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 700;
		color: #333333;
		line-height: 50rpx;
	}

	.courseCatalog_fd0_0_c0_c0 {
		font-size: 36rpx;
		color: #333;
	}

	.courseCatalog_fd0_0_c0 {
		width: 120rpx;
	}

	.courseCatalog_fd0_0 {
		padding: 0rpx 32rpx 0rpx 32rpx;
	}

	.courseCatalog_flex_1 {
		padding: 0rpx 30rpx 0rpx 30rpx;
	}

	.courseCatalog_fd1_0_c2_c1 {
		width: 40rpx;
		line-height: 40rpx;
	}

	.courseCatalog_fd1_0_c1_c0_c0_c1_c0 {
		font-size: 28rpx;
		color: rgba(16, 16, 16, 1);
	}

	.courseCatalog_fd1_0_c1_c0_c0_c1 {
		margin: 0rpx 0rpx 0rpx 16rpx;
		width: calc(100% - 56rpx);
	}

	.courseCatalog_fd1_0_c1_c0_c0_c0 {
		width: 40rpx;
		height: 40rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.courseCatalog_fd1_0_c1 {
		background: #F8F8FA;
		background-size: 100% auto;
		padding: 24rpx;
		border-radius: 16rpx;
		margin: 20rpx 0rpx 0rpx 0rpx;
	}

	.courseCatalog_fd1_0_c0_c0 {
		font-size: 32rpx;
		font-weight: 700;
		color: rgba(16, 16, 16, 1);
	}

	.courseCatalog_fd1_0_c0 {
		margin: 32rpx 0rpx 4rpx 0rpx;
	}
</style>